package cn.micro.keep.accounts.service.impl;

import cn.micro.keep.accounts.common.utils.SecurityUtil;
import cn.micro.keep.accounts.entity.AccountDimShareGroupUser;
import cn.micro.keep.accounts.entity.AccountDimUser;
import cn.micro.keep.accounts.entity.AccountOdsShareGroupRequestJoinRecord;
import cn.micro.keep.accounts.entity.dto.ReplyJoinGroupDTO;
import cn.micro.keep.accounts.entity.dto.RequestJoinGroupSearch;
import cn.micro.keep.accounts.entity.dto.ShareGroupRequestDTO;
import cn.micro.keep.accounts.entity.vo.RequestJoinGroupVO;
import cn.micro.keep.accounts.exception.MicroSysException;
import cn.micro.keep.accounts.mapper.AccountOdsShareGroupRequestJoinRecordMapper;
import cn.micro.keep.accounts.service.AccountDimShareGroupUserService;
import cn.micro.keep.accounts.service.AccountDimUserService;
import cn.micro.keep.accounts.service.AccountOdsShareGroupRequestJoinRecordService;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 邀请加入组别记录表 服务实现类
 * </p>
 *
 * @author 柘阳科技
 * @since 2023-11-10
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AccountOdsShareGroupRequestJoinRecordServiceImpl extends ServiceImpl<AccountOdsShareGroupRequestJoinRecordMapper, AccountOdsShareGroupRequestJoinRecord> implements AccountOdsShareGroupRequestJoinRecordService {

    private final AccountDimUserService userService;

    private final AccountDimShareGroupUserService shareGroupUserService;

    @Override
    @DSTransactional
    public boolean requestJoinGroup(ShareGroupRequestDTO dto) {
        AccountDimUser user = userService.getByUsernameOrPhoneOrOpenId(dto.getPhone());
        if (user == null){
            throw new MicroSysException("该用户未注册，邀请该朋友注册");
        }
        LambdaQueryWrapper<AccountDimShareGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountDimShareGroupUser::getShareGroupId,dto.getShareGroupId())
                .eq(AccountDimShareGroupUser::getUserId,user.getId());
        AccountDimShareGroupUser shareGroupUser = shareGroupUserService.getOne(queryWrapper);
        if (shareGroupUser!=null){
            throw new MicroSysException("该用户已加入该组别");
        }
        AccountOdsShareGroupRequestJoinRecord requestJoinRecord = new AccountOdsShareGroupRequestJoinRecord();
        requestJoinRecord.setRequestDesc(dto.getRequestDesc());
        requestJoinRecord.setShareGroupId(dto.getShareGroupId());
        requestJoinRecord.setSenderId(SecurityUtil.getUserId());
        requestJoinRecord.setAccepterId(user.getId());
        return this.save(requestJoinRecord);
    }

    @Override
    public boolean replyJoinGroup(ReplyJoinGroupDTO dto) {
        boolean result = false;
        LambdaQueryWrapper<AccountOdsShareGroupRequestJoinRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccountOdsShareGroupRequestJoinRecord::getAccepterId,SecurityUtil.getUserId())
                .eq(AccountOdsShareGroupRequestJoinRecord::getId,dto.getId());
        AccountOdsShareGroupRequestJoinRecord requestJoinRecord = this.getOne(queryWrapper);
        if (requestJoinRecord == null){
            throw new MicroSysException("该邀请记录不存在");
        }
        if (requestJoinRecord.getStatus() != null && requestJoinRecord.getStatus() != 0){
            throw new MicroSysException("该邀请记录已处理");
        }
        requestJoinRecord.setRequestReplyDesc(dto.getReplyDesc());
        requestJoinRecord.setStatus(dto.getStatus());
        if (this.updateById(requestJoinRecord)){
            AccountDimShareGroupUser groupUser = new AccountDimShareGroupUser();
            groupUser.setShareGroupId(requestJoinRecord.getShareGroupId());
            groupUser.setUserId(requestJoinRecord.getAccepterId());
            result = shareGroupUserService.save(groupUser);
        }
        return result;
    }

    @Override
    public List<RequestJoinGroupVO> listAcceptRequestJoinGroup(RequestJoinGroupSearch search) {
        PageHelper.startPage(search.getPageNum(),search.getPageSize());
        search.setAccepterId(SecurityUtil.getUserId());
        return this.baseMapper.queryAcceptRequestJoinGroup(search);
    }
}
